<?php

/**
	Bandit Social Module

	The contents of this file are subject to the terms of the GNU General
	Public License Version 2.0. You may not use this file except in
	compliance with the license. Any of the license terms and conditions
	can be waived if you get permission from the copyright holder.

	Copyright (c) 2011 Bandit Media
	Jermaine Marée

	Inspired by Sawyer Hollenshead's Social Bartender
	http://shakenandstirredweb.com/plugins/social-bartender/

		@package BanditSocial
		@version 1.1
**/

//! Bandit Social
class BanditSocial extends FeatherBase {

	protected static
		// Hook
		$hook,
		// Option
		$option,
		//! URL
		$url,
		//! Path
		$path;

	/**
		Initialize module
			@public
	**/
	static function init() {
		// Get Option
		self::$option = get_option('bandit_social');
		// Set default option
		if(self::$option == FALSE) { update_option('bandit_social',''); }
		// Set Path
		self::$path = FEATHER_THEME_PATH.'modules/bandit-social/';
		// Set URL
		self::$url = FEATHER_THEME_URL.'modules/bandit-social/';
		// Admin init
		add_action('admin_init',__CLASS__.'::admin_init');
	}

	/**
		Admin init
			@public
	**/
	static function admin_init() {
		// Set Hook
		self::$hook = get_plugin_page_hook('banditmod','themes.php');
		// Get Tab
		$current_tab = FeatherAdmin::get_current_tab(BanditAdmin::$module_tabs);
		// Register settings
		register_setting('bandit-social-settings','bandit_social',
			__CLASS__.'::admin_validate_settings');
		// Is module current tab?
		if('social'==$current_tab) {
			// Module styles
			add_action('admin_print_styles-'.self::$hook,__CLASS__.'::admin_styles');
			// Module scripts
			add_action('admin_print_scripts-'.self::$hook,__CLASS__.'::admin_scripts');
		}
	}

	/**
		Admin stylesheets
			@public
	**/
	static function admin_styles() {
		// Register style
		if(version_compare('3.3',get_bloginfo('version'),'>')):
			wp_register_style('bandit-social-css',self::$url.'bandit-social.css',array('thickbox','nav-menu'));
		else:
			wp_register_style('bandit-social-css',self::$url.'bandit-social.css', array('wp-admin','thickbox'));
		endif;
		// Enqueue style
		wp_enqueue_style('bandit-social-css');
	}

	/**
		Admin javascript
			@public
	**/
	static function admin_scripts() {
		// Register script
		wp_register_script('bandit-social-js',self::$url.'bandit-social.js', 
			array('jquery','media-upload','thickbox','jquery-ui-core','jquery-ui-sortable','jquery-ui-tabs'));
		// Enqueue script
		wp_enqueue_script('bandit-social-js');
	}

	/**
		Validate settings
			@public
	**/
	static function admin_validate_settings($input) {
		$action = esc_attr($input['action']);

		if($action == 'new') {
			// Get current options
			$valid=get_option('bandit_social');
			// Any options exist?
			if(!$valid) { $valid = array(); }
			// Validate input
			$tmp['url'] = esc_url($input['url']);
			$tmp['name'] = esc_attr($input['name']);
			// Icon
			$split = get_template_directory_uri().'/feather/modules/bandit-social/icons/';
			$icon = explode($split,esc_url($input['icon']));
			$tmp['icon'] = isset($icon[1])?$icon[1]:esc_url($input['icon']);

			// Add to array
			$valid[]=$tmp;
			// Settings saved notice
			add_settings_error('feather_setting_notices',
				esc_attr('settings_updated'),__('Item created.'),'updated');
			// Return validated settings
			return $valid;
		}

		if($action == 'update') {
			// Unset action
			unset($input['action']);
			// Create valid array
			$valid = array();
			// Loop through items for update
			foreach($input as $item) {
				$item['url'] = esc_url($item['url']);
				$item['name'] = esc_attr($item['name']);
				// Icon
				if(substr($item['icon'],0,4)=='http') {
					$item['icon'] = esc_url($item['icon']);
				} else {
					$item['icon'] = esc_attr($item['icon']);	
				}
				// Add to valid array
				$valid[]=$item;
				// Unset item
				unset($item);
			}
			// Settings saved notice
			add_settings_error('feather_setting_notices',
				esc_attr('settings_updated'),__('List updated.'),'updated');
			// Return validated settings
			return $valid;
		}
	}

	/**
		Get icons
			@public
	**/
	static function get_icons($path='default') {
		// Get images
		$icons = scandir(self::$path.'icons/'.$path);
		// Return images
		if($icons)
			return $icons;
		else
			return FALSE;
	}

	/**
		Default icon list
			@public
	**/
	static function default_icon_list() {
		// Get icons
		$icons = self::get_icons();
		// Build list
		if($icons) {
			$output = '<ul class="bandit-icons">';
			// Loop through icons
			foreach($icons as $icon) {
				if($icon != "." && $icon != "..")
					$output .= '<li><img src="'.self::$url.'icons/default/'.$icon.'" alt="'.$icon.'" title="'.$icon.'" />';
			}
			$output .= '</ul>';
			// Return list
			return $output;
		}
	}

	/**
		Get items
			@public
	**/
	static function get_items() {
		$options = self::$option;
		$url = get_template_directory_uri().'/feather/modules/bandit-social/icons/';
		// Loop through items
		foreach($options as $key=>$item) {
			$options[$key]['icon_input'] = $options[$key]['icon'];
			// Icon
			if(substr($options[$key]['icon'],0,4)!='http') {
				$options[$key]['icon'] = $url.$options[$key]['icon'];
			}
		}
		return $options;
	}

}